package com.hilaver.dzmis.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.codetable.AbstractCodeTable;
import com.hilaver.dzmis.dao.BaseHibernateDAO;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.service.ICtService;
import com.hilaver.dzmis.util.StringUtils;

public class CtServiceImpl extends AbstractBaseService{
	
	public final static String PACKAGE_NAME = "com.hilaver.dzmis.codetable";
	
	public final static String ORDER_BY = "code";
	
	public final static String COLUMN_DESCRIPTION_FR = "descriptionFr";
	
	public final static String COLUMN_DESCRIPTION_ZH = "descriptionCn";
	
	public final static String COLUMN_DESCRIPTION_EN = "descriptionEn";
	
	public final static String SPECIAL_CLAZZ_CT_YESNO = "ctYesno";
	
	
	
	public static Map<String, String> CT_MAP = new HashMap<String, String>();
	
	private BaseHibernateDAO dao;
	
	public CtServiceImpl(){
		this.dao = new BaseHibernateDAO();
	}
	
	public String getAll(String className, Locale locale) throws Exception {
		
		if(CT_MAP.containsKey(className + Constants.KEY_SEPARATOR + locale.toString().toLowerCase())){
			return CT_MAP.get(className + Constants.KEY_SEPARATOR + locale.toString().toLowerCase());
		}
		List ctList = this.dao.getAll(className, ORDER_BY, Constants.ORDER_ASC);
		StringBuffer xmlSB = new StringBuffer();

		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_LOCALE_TAG[0]);
		xmlSB.append(locale.toString().toLowerCase());
		xmlSB.append(Constants.XML_LOCALE_TAG[1]);
		for(Iterator iterator = ctList.iterator(); iterator.hasNext();){
			AbstractCodeTable ct = (AbstractCodeTable)iterator.next();
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_CODE_TAG[0]);
			xmlSB.append(ct.getCode());
			xmlSB.append(Constants.XML_CODE_TAG[1]);
			xmlSB.append(Constants.XML_DESCRIPTION_TAG[0]);
			
			xmlSB.append(Constants.XML_CDATA[0]);
			if(Constants.LOCALE_TYPE_EN.equals(locale)){
				xmlSB.append(ct.getDescriptionEn());
			}else if(Constants.LOCALE_TYPE_ZH.equals(locale)){
				xmlSB.append(ct.getDescriptionCn());
			}else{
				xmlSB.append(ct.getDescriptionFr());
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			
			xmlSB.append(Constants.XML_DESCRIPTION_TAG[1]);
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(ctList.size());
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);

		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		
		String rtn = xmlSB.toString();
		CT_MAP.put(className + Constants.KEY_SEPARATOR + locale.toString().toLowerCase(), rtn);
		return rtn;
	}

	@Override
	public String delete(int id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String get(int id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getDescription(String className, String code, Locale locale) throws Exception{
		if(StringUtils.isEmpty(code)){
			return "";
		}
		
		AbstractCodeTable act = (AbstractCodeTable)this.dao.get(className, code);
		String rtn;
		if(Constants.LOCALE_TYPE_EN.equals(locale)){
			rtn = act.getDescriptionEn();
		}else if(Constants.LOCALE_TYPE_ZH.equals(locale)){
			rtn = act.getDescriptionCn();
		}else{
			rtn = act.getDescriptionFr();
		}
		if(rtn.indexOf(" = ") != -1){
			rtn = rtn.substring(0, rtn.indexOf(" = "));
		}
		return rtn;
	}
	
	public String getCode(String className, String description, Locale locale) throws Exception{
		String propertyName;
		if(Constants.LOCALE_TYPE_EN.equals(locale)){
			propertyName = COLUMN_DESCRIPTION_EN;
		}else if(Constants.LOCALE_TYPE_ZH.equals(locale)){
			propertyName = COLUMN_DESCRIPTION_ZH;
		}else{
			propertyName = COLUMN_DESCRIPTION_FR;
		}
		List rtnList = this.dao.findByProperty(className, propertyName, description);
		if(rtnList.size() > 0){
			AbstractCodeTable act = (AbstractCodeTable)rtnList.get(0);
			return act.getCode();
		}else{
			return null;
		}
	}


}
